package cn.edu.scau.cmi.wangzm.composite.consistent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class Controller {
	@FXML
    private TextField inputField1;
	@FXML
    private TextField inputField2;
	@FXML
    private TextField inputField3;
	@FXML
    private TextField inputField4;
    @FXML
    private Button button;
    @FXML
    private TextArea outputArea;
    List<ConsistentComponent> Team=new ArrayList<ConsistentComponent>();
	ConsistentComponent trunk=new ConsistentComposite();
    @FXML
    void handleOk(ActionEvent event) {
    	outputArea.clear();
    	int count=1;
    	int key=1;
    	if(inputField1.getText().isEmpty()) {outputArea.appendText("请输入选项!\n"); key=0;}
    	else if(inputField1.getText().equals("1")){
    		if(inputField2.getText().isEmpty()||inputField3.getText().isEmpty()) {outputArea.appendText("请输入完整信息!\n");key=0;}
    		else {
    			String name=inputField2.getText();
    			ConsistentComponent firstLeaf=new ConsistentLeaf();
			      ((ConsistentLeaf)firstLeaf).setName(name);
			    String teamname=inputField3.getText();
			      if(Team.isEmpty()) {
			    	  ConsistentComponent Branch=new ConsistentComposite();
			          ((ConsistentComposite)Branch).setName(teamname);
			          Branch.addChild(firstLeaf);
			          Team.add(Branch);
			      }
			      else {
			      for(int i=0;i<Team.size();i++) {
			    	  if(teamname.equals(((ConsistentComposite)Team.get(i)).getName())) {
			    		  Team.get(i).addChild(firstLeaf);
			    		  count=0;
			    	  }
			      }
			    	  if(count==1) {
			    		  ConsistentComponent Branch=new ConsistentComposite();
				          ((ConsistentComposite)Branch).setName(teamname);
				          Branch.addChild(firstLeaf);
				          Team.add(Branch);
			    	  }
			      }
    		}
    	}
    	else if(inputField1.getText().equals("2")){
    		if(inputField4.getText().isEmpty()) {outputArea.appendText("请输入完整信息!\n");key=0;}
    		else {
    			String teamname1=inputField4.getText();
		    	  if(Team.isEmpty()) {
		    		  ConsistentComponent Branch=new ConsistentComposite();
			          ((ConsistentComposite)Branch).setName(teamname1);
			          Team.add(Branch);
			      }
		    	  else {
		    	  for(int i=0;i<Team.size();i++) {
			    	  if(teamname1.equals(((ConsistentComposite)(Team.get(i))).getName())) {
			    		  outputArea.appendText("该团队名已经被创建!\n");
			    		  count=0;
			    	  }
		    	  }
			    	  if(count==1) {
			    		  ConsistentComponent Branch=new ConsistentComposite();
				          ((ConsistentComposite)Branch).setName(teamname1);
				          Team.add(Branch);
			    	  }
		    	  }
    		}
    	}
    	else {outputArea.appendText("请输入正确选项!\n");key=0;}
    	if(key==1) {
    	for(int i=0;i<Team.size();i++) {
            trunk.addChild(Team.get(i));
        }
    	Set<ConsistentComponent> trunkChildren = trunk.getChildren();
  		Iterator<ConsistentComponent> trunkChildrenIterator = trunkChildren.iterator();
		outputArea.appendText(((ConsistentComposite)trunk).getName()+"\n");
		while(trunkChildrenIterator.hasNext()) {
			ConsistentComposite text=new ConsistentComposite();
  			text=(ConsistentComposite)trunkChildrenIterator.next();
			outputArea.appendText("      "+text.getName()+"\n");
			if(text.getChildren()!=null) {
				Set<ConsistentComponent> BranchChildren = text.getChildren();
  				Iterator<ConsistentComponent> BranchChildrenIterator = BranchChildren.iterator();
				while(BranchChildrenIterator.hasNext()) {
					outputArea.appendText("           "+((ConsistentLeaf)BranchChildrenIterator.next()).getName()+"\n");
				}
			}
			}
    	}
    }
}
